package com.kmsoft.provider.facade.controller;

import cn.hutool.json.JSONUtil;
import com.kmsoft.provider.facade.config.ConfigValue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("web")
public class ProviderFacade {
    @Value("${spring.application.name:}")
    private String applicationName;
    @Value("${km.nacosconfig:}")
    private String nacosconfig;
    @Value("${km.nacosprovider:}")
    private String nacosprovider;
    @Value("${km.cloudconfig:}")
    private String cloudconfig;
    @Value("${km.local:}")
    private String local;
    @Resource
    private ConfigValue configValue;
    @Resource
    private Environment environment;

    @GetMapping("/test")
    public String test() {
        log.info("================local service:{}", applicationName);
        String value = String.format("@value(local):%s,\n@value(nacosconfig):%s,\n@value(nacosprovider):%s,\n@value(cloudconfig):%s,\n" +
                        "env.nacosconfig:%s,\nenv.nacosprovider:%s,\nenv.cloudconfig:%s,\n" +
                        "ConfigurationProperties:%s",
                local, nacosconfig, nacosprovider, cloudconfig,
                environment.getProperty("km.nacosprovider"), environment.getProperty("km.nacosconfig"), environment.getProperty("km.cloudconfig"),
                JSONUtil.toJsonStr(configValue));
        return value;
    }

    @GetMapping("/test/{name}")
    public String test(@PathVariable String name) {
        return environment.getProperty(name);
    }
}
